<?php 
/*
Plugin Name: RSS-WORLD
Plugin URI: http://yourdomain.com/
Description: A xml-rss world wordpress plugin
Version: 1.0
Author: Viktor Karlsson, Jarmo Baltzar
Author URI: http://yourdomain.com
License: GPL
*/

include_once "view/NewsView.php";
include_once "model/iScreenScraper.php";
include_once "model/ScreenScraper.php";
include_once "model/CurlScraper.php";

/* Plugin provided by Marta */
global $my_plugin_table;
global $my_plugin_db_version;
global $wpdb;
	
$my_plugin_table = $wpdb->prefix . 'my_plugin';
$my_plugin_db_version = '1.0';

register_activation_hook(__FILE__, 'my_plugin_install');



function my_plugin_install()
{
	global $wpdb;
	global $my_plugin_table;
	global $my_plugin_db_version;
	
	if($wpdb->get_var("show tables like '$my_plugin_table'") != $my_plugin_table){
	$sql = "CREATE TABLE $my_plugin_table (".
	"id int NOT NULL AUTO_INCREMENT , ".
	"user_text varchar(500) NOT NULL, ".
	"UNIQUE KEY id (id) ".")";
	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	dbDelta($sql);
	
	$wpdb->query("INSERT INTO $my_plugin_table (user_text) VALUES ('http://www.fz.se/core/rss/fznews_rss20.xml')");

	add_option("my_plugin_db_version", $my_plugin_db_version);
	}

}


class RssWorldWidget extends WP_Widget
{
  function RssWorldWidget()
  {
    $widget_ops = array('classname' => 'RssWorldWidget', 'description' => 'Display an xml rss set in options' );
    $this->WP_Widget('RssWorldWidget', 'Random Xml and Rss', $widget_ops);
  }
 
  function form($instance)
  {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    $title = $instance['title'];
?>
  <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" 
  name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<?php
  }
 
  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance['title'] = $new_instance['title'];
    return $instance;
  }
 
  function widget($args, $instance)
  {
    extract($args, EXTR_SKIP);
 
    echo $before_widget;
    $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
 
    if (!empty($title))
      echo $before_title . $title . $after_title;;
 
    // Code
    global $wpdb;
	
	$scraping = new ScreenScraper();
	
	$table_name = $wpdb->prefix . "my_plugin";
	$sql = "SELECT * FROM ". $table_name . " WHERE id = 1";
    $url = $wpdb->get_row($sql);

	$xml = $scraping->DoScrap($url->user_text);
	$view = new NewsView();
	echo $view->RSSNews($xml);
 
    echo $after_widget;
  }
 
}
add_action( 'widgets_init', create_function('', 'return register_widget("RssWorldWidget");') );


if(is_admin){
	add_action('admin_menu', 'Rss_world_admin_menu');
	
	function Rss_world_admin_Menu(){
		add_options_page('Rss-World', 'Rss-World', 'administrator', 'Rss-World', 'Rss_World_Html');
	}
}

function Rss_World_Html(){
?>	


<div>
	<h2>Rss-world options</h2>

	<?php 
		if(isset($_POST['rss_url_data'])){
			echo $_POST['rss_url_data'];
			
			global $wpdb;
			global $my_plugin_table;
			
			$sql = "UPDATE $my_plugin_table
					SET user_text=". $_POST['rss_url_data'] .
					"WHERE id = 1";
			
			$wpdb->update($my_plugin_table, array('user_text' => $_POST['rss_url_data']), array('id' => 1));
		}
	?>

	<form method="post" action="options-general.php?page=Rss-World">
	<?php wp_nonce_field('update-options'); ?>

		<p>
			<input name="rss_url_data" type="text" id="rss_url_data" /><strong>example, xml.com</strong>
		</p> 

		<p>
			<input type="submit" value="<?php _e("Save Changes") ?>" />
		</p>

	</form>
</div>
<?php
}
?>
      
